/**
 * Created by WYH on 17/4/25.
 */
angular.module('rentAdd', []).component(
  'rentAdd',
  {
    templateUrl: 'component/rent/rentAdd/rent_add.template.html',
    controller: ['$scope', '$rootScope', 'Api', 'RentProtocolModel', 'Alert', function ($scope, $rootScope, Api, RentProtocolModel, Alert) {
      
      getCompanyDetail();
      
      $scope.rentProtocol = new RentProtocolModel();
      
      $scope.onTimeSetStart = onTimeSetStart;
      
      $scope.onTimeSetEnd = onTimeSetEnd;
      
      $scope.submitProtocol = submitProtocol;
      
      $scope.checkCountPayCycle = checkCountPayCycle;
      
      $scope.checkCountMonthlyRent = checkCountMonthlyRent;
      
      $scope.changeCompany = function(){
        if(!$scope.rentProtocol.manager) return;
        Api.getRentEndTime($scope.rentProtocol.manager.id,function(limitTime){
          $scope.showStartTime = false;
          $scope.showEndTime = false;
          if(limitTime){
            $scope.limitTime = limitTime + 24*60*60*1000;
            $scope.data = {data: $scope.limitTime} ;
            $scope.rentProtocol.beginTime = $scope.limitTime;
            if(!$scope.rentProtocol.endTime ||$scope.rentProtocol.beginTime > $scope.rentProtocol.endTime ){
              $scope.rentProtocol.endTime = undefined;
            }
          }else{
            $scope.data = {data: undefined} ;
            $scope.rentProtocol.beginTime = undefined;
            $scope.limitTime = undefined;
          }
         
          
        })
      };
     
      
      function checkCountPayCycle(value) {
        if (value < 0) {
          Alert.alertBox('输入值不可为负数')
          $scope.rentProtocol.payCycle = 0
        }
      }
      
      function checkCountMonthlyRent(value) {
        if (value < 0) {
          Alert.alertBox('输入值不可为负数')
          $scope.rentProtocol.monthlyRent = 0
        }
      }
      
      
      function submitProtocol(rentProtocol) {
        rentProtocol.type = "OFFICE";
        if (rentProtocol.beginTime > rentProtocol.endTime) {
          return Alert.alertBox("请确认租期填写正确")
        }
        if (!rentProtocol.manager) {
          return Alert.alertBox('请选择企业信息')
        }
        
        rentProtocol.ownerId = rentProtocol.manager.id;
        Api.createRentProtocol(rentProtocol, function (result) {
          Alert.alertBox('创建协议成功', function () {
            return window.location.reload();
          });
        });
      }
      
      
      function onTimeSetStart(newDate, oldDate) {
        $scope.rentProtocol.beginTime = newDate.getTime();
        $scope.showStartTime = false;
      }
      
      function onTimeSetEnd(newDate, oldDate) {
        $scope.rentProtocol.endTime = newDate.getTime();
        $scope.showEndTime = false;
      }
      
      function getCompanyDetail() {
        $rootScope.$watch('RootCompanyInfo', function (newCompanyInfo) {
          if (newCompanyInfo && newCompanyInfo.id) {
            $scope.companyDetail = newCompanyInfo;
            getCompanyList();
          }
        })
      }
      
      
      function getCompanyList() {
        var communityId = $scope.companyDetail.communityId;
        Api.getCompanyList(communityId, function (result) {
          $scope.companyList = result.data;
        })
      }
      
      
      Date.prototype.setIntYear = function () {
        this.setUTCMonth(0);
        this.setUTCDate(1);
        this.setUTCHours(0, 0, 0, 0)
      };
      Date.prototype.setIntMonth = function () {
        this.setUTCDate(1);
        this.setUTCHours(0, 0, 0, 0)
      };
      Date.prototype.setIntDay = function () {
        this.setUTCHours(this.getUTCHours(), 0, 0, 0)
      };
      
      $scope.beginDateBeforeRender = function ($view, $dates) {
        console.log("------clear")
        var limitDate
        if($scope.limitTime){
          limitDate =  new Date($scope.limitTime)
        }else{
          limitDate = new Date()
  
          limitDate.setIntMonth()
          limitDate.setMonth(limitDate.getMonth() - 1)
        }
        
        var todaySinceMidnight = new Date($scope.rentProtocol.endTime);
        
        
        if ($view == 'year') {
          todaySinceMidnight.setIntYear()
          limitDate.setIntYear()
        }
        if ($view == 'month') {
          todaySinceMidnight.setIntMonth()
          limitDate.setIntMonth()
        }
        if ($view == 'day') {
          todaySinceMidnight.setIntDay()
          limitDate.setIntDay()
        }
        
        
        $dates.filter(function (date) {
          
          if ($view == 'day') {
            return date.utcDateValue >= todaySinceMidnight.getTime() || date.utcDateValue < limitDate.getTime()
          }
          return (date.utcDateValue < limitDate.getTime() || date.utcDateValue > todaySinceMidnight.getTime());
          
        }).forEach(function (date) {
          // console.log(date.utcDateValue)
          date.selectable = false;
        });
        
        $dates.filter(function (date) {
          if ($view == 'day') {
            
            return date.utcDateValue < todaySinceMidnight.getTime() && date.utcDateValue >= limitDate.getTime()
          }
          return date.utcDateValue <= todaySinceMidnight.getTime() && date.utcDateValue >= limitDate.getTime();
        }).forEach(function (date) {
          date.selectable = true;
        });
      };
      
      
      $scope.endDateBeforeRender = function ($view, $dates) {
        var limitDate;
        var todaySinceMidnight = new Date($scope.rentProtocol.beginTime);
        if($scope.limitTime){
          limitDate =  new Date($scope.limitTime)
        }else{
          limitDate = new Date()
    
          limitDate.setIntMonth()
          limitDate.setMonth(limitDate.getMonth() - 1)
        }
        
        // var limitDate = new Date();
        // limitDate.setUTCMonth(limitDate.getMonth() - 1);
        // limitDate.setIntMonth();
        
        if ($view == 'year') {
          todaySinceMidnight.setIntYear();
          limitDate.setIntYear()
        }
        if ($view == 'month') {
          todaySinceMidnight.setIntMonth();
          limitDate.setIntMonth()
        }
        if ($view == 'day') {
          todaySinceMidnight.setIntDay();
          limitDate.setIntDay()
          todaySinceMidnight.setHours(todaySinceMidnight.getDate() + 8)
        }
        //console.log(todaySinceMidnight.getTime())
        
        $dates.filter(function (date) {
          if ($view == 'day') {
            return date.utcDateValue <= todaySinceMidnight.getTime() || date.utcDateValue <= limitDate.getTime();
          }
          return date.utcDateValue < todaySinceMidnight.getTime() || date.utcDateValue < limitDate.getTime();
          
          
        }).forEach(function (date) {
          
          date.selectable = false;
        });
        $dates.filter(function (date) {
          if ($view == 'day') {
            return date.utcDateValue > todaySinceMidnight.getTime() && date.utcDateValue > limitDate.getTime();
          } else {
            return date.utcDateValue >= todaySinceMidnight.getTime() && date.utcDateValue >= limitDate.getTime();
          }
          
        }).forEach(function (date) {
          
          
          date.selectable = true;
        });
      };
      
      
    }]
  }
);